package tr.edu.ege.seagent.hobnobwebapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import net.sf.json.JSONObject;

import tr.edu.ege.seagent.hobnobontology.instance.HobNobResultSetSolution;
import tr.edu.ege.seagent.hobnobontology.instance.HobNobSPARQLQueries;

/**
 * Servlet implementation class AndroidFindLocationOfNotification
 */
@WebServlet("/AndroidLocation")
public class AndroidLocationOfTheNotification extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AndroidLocationOfTheNotification() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		// Getting resourceURI as a parameter...
		String resourceURI = request.getParameter("resourceURI");

		List<HobNobResultSetSolution> resultSet = getLocationResult(resourceURI);

		Place place = new Place();
		place.setPlaceURIs((ArrayList<String>) getPlaces(resultSet));
		// writes result as a json object...
		out.print(new Gson().toJson(place).toString());

	}

	/**
	 * Gets locations from the
	 * {@link HobNobSPARQLQueries#findLocationOfNotificationOverHttp(String)}
	 * query.
	 * 
	 * @param resourceURI
	 *            URI of notification that we want to learn its location.
	 * @return resultSet of query.
	 */
	private List<HobNobResultSetSolution> getLocationResult(String resourceURI) {
		// find linkedgeodata locations...
		HobNobSPARQLQueries query = new HobNobSPARQLQueries();
		List<HobNobResultSetSolution> resultSet = query
				.findLocationOfNotificationOverHttp(resourceURI);
		return resultSet;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * Traverses the given result set and collects place URIs.
	 * 
	 * @param resultSet
	 *            result of the linkedgeodata place query.
	 * @return a list of place URIs which are not repeated.
	 */
	private List<String> getPlaces(List<HobNobResultSetSolution> resultSet) {
		List<String> placeURIList = new ArrayList<String>();
		for (HobNobResultSetSolution result : resultSet) {
			String placeURI = result.getLocation().asResource().getURI();
			if (!placeURIList.contains(placeURI)) {
				placeURIList.add(placeURI);
			}
		}
		return placeURIList;
	}

}
